package mq.demo1;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

public class QueueConsumerDemo {
    public static void main(String[] args) throws JMSException, IOException {
        String brokerURL="tcp://192.168.100.100:61616";
        // 工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
        // 连接对象
        Connection connection = activeMQConnectionFactory.createConnection("admin", "admin");
        // 连接
        connection.start();
        // 第一个参数: 是否开启事务,第二个参数:应答模式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue testQueue = session.createQueue("test_queue");

        // 创建消费者
        MessageConsumer consumer = session.createConsumer(testQueue);
        TextMessage message = (TextMessage) consumer.receive();
        System.out.println("收到消息:" + message.getText() );

        System.in.read();
        consumer.close();
        session.close();
        connection.close();
    }
}
